package com.ttc.ssm.service.admin.impl;

import com.ttc.ssm.common.CompanyPage;
import com.ttc.ssm.mapper.admin.*;
import com.ttc.ssm.po.*;
import com.ttc.ssm.service.admin.JobService;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Jiarui on 2016/7/29.
 */
public class JobServiceImpl implements JobService {

    @Autowired
    JobMapper jobMapper;

    @Autowired
    JobTypeMapper jobTypeMapper;

    @Autowired
    JobPropertyMapper jobPropertyMapper;

    @Autowired
    DegreeMapper degreeMapper;

    @Autowired
    SalaryTypeMapper salaryTypeMapper;

    @Autowired
    JobTagMapper jobTagMapper;

    @Autowired
    TagToJobMapper tagToJobMapper;

    @Override
    public List<Job> selectJobForPage(CompanyPage page) throws Exception {
        return jobMapper.selectJobForPage(page);
    }

    @Override
    public void deleteJobs(Map<String, Object> map) throws Exception {
        jobMapper.deleteJobs(map);
        tagToJobMapper.deleteTagIn(map);
    }

    @Override
    public void updateJobStatus(int job_id, int status) throws Exception {
        Date d = new Date();
        String s;
        java.text.DateFormat format1 = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        s = format1.format(d);
        jobMapper.updateJobStatus(job_id,status,s);
    }

    @Override
    public int countRows(CompanyPage companyPage) throws Exception {
        return jobMapper.countRows(companyPage);
    }

    @Override
    public List<SalaryType> getSalaryType() throws Exception {
        return salaryTypeMapper.getSalaryType();
    }

    @Override
    public List<JobType> getJobType() throws Exception {
        return jobTypeMapper.getJobType();
    }

    @Override
    public List<JobProperty> getJobProperty() throws Exception {
        return jobPropertyMapper.getJobProperty();
    }

    @Override
    public List<Degree> getDegree() throws Exception {
        return degreeMapper.getDegree();
    }

    @Override
    public Job selectJobById(int job_id) throws Exception {
        return jobMapper.selectJobById(job_id);
    }

    @Override
    public List<JobTag> getJobTag(int company_id) throws Exception {
        return jobTagMapper.getJobTag(company_id);
    }

    @Override
    public List<TagToJob> getTagForJob(int job_id) throws Exception {
        return tagToJobMapper.getTagForJob(job_id);
    }

    @Override
    public void updateJob(Job job) throws Exception {
        jobMapper.updateJob(job);
        Map map = new HashMap();
        map.put("job_id",job.getJob_id());
        map.put("tag",job.getTag());
        if(job.getTag()!=null) {
            int length = tagToJobMapper.checkTag(map);
            System.out.println(length + " " + job.getTag().size());
            if (length != job.getTag().size()) {
                tagToJobMapper.deleteTag(job.getJob_id());
                tagToJobMapper.insertTag(map);
            } else if (length != 0) {
                tagToJobMapper.deleteTagNotIn(map);
            }
        }else{
            tagToJobMapper.deleteTag(job.getJob_id());
        }
    }
}
